<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 媒体控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\service\MediaService;
use think\Request;

class MediaController extends VerifyController
{
    /**
     * @var \common\service\MediaService
     */
    protected $mediaService;

    public function __construct()
    {
        parent::__construct();
        $this->mediaService = MediaService::getInstance();
    }

    /**
     * 添加媒体指定上传服务器配置信息
     * @param Request $request
     * @return \think\Response
     */
    public function addMediaUploadServer(Request $request)
    {
        $param = $request->param();
        $resData = $this->mediaService->addMediaServer($param['data']);
        return json($resData);
    }

    /**
     * 获取媒体指定上传服务器配置信息
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function getMediaUploadServer(Request $request)
    {
        $resData = teamones_request('teamones-im', 'options/get_media_upload_server', null)['data'];
        if (is_array($resData)) {
            $resData['belong_system'] = C('belong_system');
        }
        return json(success_response('', $resData));
    }

    /**
     * 获取指定服务器信息
     * @param Request $request
     * @return \think\Response
     */
    public function getMediaServerItem(Request $request)
    {
        $param = $request->param();
        $resData = $this->mediaService->getMediaServerItem($param);
        return json($resData);
    }

    /**
     * 获取指定媒体信息
     * @param Request $request
     * @return \think\Response
     */
    public function getMediaData(Request $request)
    {
        $param = $request->param();
        $resData = $this->mediaService->getMediaData($param);
        return json($resData);
    }

    /**
     * 获取所有媒体服务器状态
     * @param Request $request
     * @return \think\Response
     */
    public function getMediaServerStatus(Request $request)
    {
        $param = $request->param();
        $resData = $this->mediaService->getMediaServerStatus();
        return json($resData);
    }

    /**
     * 获取指定尺寸的媒体缩略图路径
     * @param Request $request
     * @return \think\Response
     */
    public function getSpecifySizeThumbPath(Request $request)
    {
        $param = $request->param();
        $resData = $this->mediaService->getSpecifySizeThumbPath($param["filter"], $param["size"]);
        return json($resData);
    }

    /**
     * 获取多个媒体信息
     * @param Request $request
     * @return \think\Response
     */
    public function selectMediaData(Request $request)
    {
        $param = $request->param();
        $resData = $this->mediaService->selectMediaData($param["server_id"], $param["md5_name_list"]);
        return json($resData);
    }

    /**
     * 保存媒体信息
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function saveMediaData(Request $request)
    {
        $param = $request->param();
        $resData = $this->mediaService->saveMediaData($param);
        return json($resData);
    }

    /**
     * 清除媒体缩略图
     * @param Request $request
     * @return \think\Response
     */
    public function clearMediaThumbnail(Request $request)
    {
        $param = $request->param();
        $resData = $this->mediaService->batchClearMediaThumbnail($param);
        return json($resData);
    }
}
